package com.rzhd.coursepatriarch.common.utils;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

/**
 * @author yql
 * Time: 2018/6/5.
 */
public class FragmentCustomManager {
    private static final String TAG = "FragmentCustomManager";

    private Activity mContent;
    private FragmentManager mFragmentManager;
    private Fragment mCurrentFragment = null;

    public FragmentCustomManager(FragmentManager fragmentManager) {
        mFragmentManager = fragmentManager;
    }

    public Fragment showFragment(int resourceId, Class<? extends Fragment> clazz ) {
        return showFragment( resourceId , clazz , null );
    }

    public Fragment showFragment(int resourceId, Class<? extends Fragment> clazz , Bundle arguments ) {
        //判断当前的fragment与需要提花你的fragment是否是同一一个
        if (!(mCurrentFragment != null && mCurrentFragment.getClass().getSimpleName().equals(clazz.getSimpleName()))) {
            //解决办法也就是把manager和transaction定义为函数内部的局部变量
            //java.lang.IllegalStateException: commit already called
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            //
            Fragment fragment = null;
            try {
                Fragment fragmentByTag = mFragmentManager.findFragmentByTag(clazz.getSimpleName());
                if (fragmentByTag != null) {
                    fragmentByTag.setArguments( arguments );
                    //显示需要显示的fragment
                    transaction.show(fragmentByTag);
                    //隐藏之前的fragment
                    transaction.hide(mCurrentFragment);
                    //记录当前的fragment为显示的fragment
                    mCurrentFragment = fragmentByTag;
                } else {
                    //通过无参的 公开的构造方法来创建fragment实例
                    fragment = clazz.newInstance();
                    //当前的fragment没有添加过 把fragment添加到manager里边
                    transaction.add(resourceId, fragment, clazz.getSimpleName());
                    if (mCurrentFragment != null) {
                        //隐藏当前的fragment
                        transaction.hide(mCurrentFragment);
                    }
                    //记录当前的ftagment为显示的fragment
                    mCurrentFragment = fragment;
                }
                //防止setNextAnim 为空
                transaction.addToBackStack(null);
//                transaction.commit();
                transaction.commitAllowingStateLoss();

            } catch (Exception e) {
                        //通过无参的 公开的构造方法来创建fragment实例
                        try {
                            fragment = clazz.newInstance();
                        //当前的fragment没有添加过 把fragment添加到manager里边
                        transaction.add(resourceId, fragment, clazz.getSimpleName());
                        if (mCurrentFragment != null) {
                            //隐藏当前的fragment
                            transaction.hide(mCurrentFragment);
                        }
                        //记录当前的ftagment为显示的fragment
                        mCurrentFragment = fragment;
                        //防止setNextAnim 为空
                        transaction.addToBackStack(null);
        //                transaction.commit();
                        transaction.commitAllowingStateLoss();
                        } catch (IllegalAccessException e1) {
                            e1.printStackTrace();
                        } catch (InstantiationException e1) {
                            e1.printStackTrace();
                        }

            }

//            } catch (InstantiationException e) {
//                e.printStackTrace();
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
        }
        return mCurrentFragment;
    }

    public void makefragment(int resourceId, Class<? extends Fragment> clazz) {
        try {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            Fragment   fragment = clazz.newInstance();
            //当前的fragment没有添加过 把fragment添加到manager里边
            transaction.add(resourceId, fragment, clazz.getSimpleName());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }

    public void setCurrentFragment(Fragment currentFragment) {
        mCurrentFragment = currentFragment;
    }
}
